﻿// Copyright (c) 2010 Dan Popick

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.IO;
using System.Linq;
using System.Text;

using Open.Web.Stijl;
using Open.Web.Stijl.Configuration;
using Open.Web.Stijl.Css;
using Open.Web.Stijl.Utility;

using StijlBilder.Configuration;

namespace StijlBilder
{
    class Program
    {
        static void Main(string[] args)
        {
			log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log4net.xml"));
	
			// process the arguments
			List<string> remainingArgs;
			StijlConfiguration config = ConfigUtility.ExtractFromCommandLine(args, out remainingArgs);

			if (remainingArgs.Count < 2)
			{
				Console.WriteLine("usage: stijlbilder [-arg [value]]* outputdir file [file]*");
				return;
			}

			// ensure the directory
			string outputDir = remainingArgs[0];
			Directory.CreateDirectory(outputDir);

			string miniCss;
			List<string> dependentFiles;
			List<TiledImage> images;
			TaskTileEngine engine = new TaskTileEngine();

			// loop over arguments
			foreach (string file in remainingArgs.Skip(1))
			{
				engine.ProcessFile(file, out  miniCss, out images, out dependentFiles);

				// encode the CSS file
				string fileName = Path.GetFileNameWithoutExtension(file);
				string ext = Path.GetExtension(file);
				byte[] cssData = Encoding.UTF8.GetBytes(miniCss);

				// compute CRC
				Open.Web.Stijl.Utility.Crc32 crcComp = new Open.Web.Stijl.Utility.Crc32();
				crcComp.ComputeHash(cssData);

				// save CSS file
				string cssPath = Path.Combine(outputDir, engine.CssFileName);
				File.WriteAllBytes(cssPath, cssData);
				Console.WriteLine("Writing out new CSS file as " + cssPath);

				// write out the images
				for (int i = 0; i < images.Count; ++i)
				{
					string imFileName = Path.Combine(outputDir, images[i].FileName);
					Directory.CreateDirectory(Path.GetDirectoryName(imFileName));
					File.WriteAllBytes(imFileName, images[i].ImageData);
					Console.WriteLine("Generated image: " + imFileName);
				}
			}
        }

    }
}
